package com.beiji.lib.pen.ui.renderer

import android.graphics.Matrix
import com.beiji.lib.pen.model.DotUnit
import com.beiji.lib.pen.model.PenDot

/**
 * 笔迹缩放辅助类
 * Created by X on 2018/6/26.
 */
object StrokeTransformHelper {

    /**
     *对PenPoint应用matrix变换
     */
    fun transformPenPoint(dot: PenDot, matrix: Matrix): PenDot {
        val transformedDot = dot.copy()
        val coordinateArray: FloatArray = floatArrayOf(dot.x.toFloat(), dot.y.toFloat())
        matrix.mapPoints(coordinateArray)
        transformedDot.x = coordinateArray[0]
        transformedDot.y = coordinateArray[1]
        return transformedDot
    }

    /**
     * 对dot应用matrix变换
     */
    fun transformDot(dot: DotUnit, matrix: Matrix): DotUnit {
        val transformedDot = dot.copy()
        val coordinateArray: FloatArray = floatArrayOf(dot.x.toFloat(), dot.y.toFloat())
        matrix.mapPoints(coordinateArray)
        transformedDot.x = coordinateArray[0]
        transformedDot.y = coordinateArray[1]
        return transformedDot
    }

    /**
     * 对dot应用matrix的逆矩阵变换
     */
    fun invertDot(dot: DotUnit, matrix: Matrix): DotUnit {
        val invertedMatrix = Matrix().apply {
            matrix.invert(this)
        }
        val transformedDot = dot.copy()
        val coordinateArray: FloatArray = floatArrayOf(dot.x.toFloat(), dot.y.toFloat())
        invertedMatrix.mapPoints(coordinateArray)
        transformedDot.x = coordinateArray[0]
        transformedDot.y = coordinateArray[1]
        return transformedDot
    }
}